<template>
  <el-dialog
    title="参与详情"
    v-model="visible"
    width="850px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="用户" prop="nickName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.nickName"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.userName"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        size="default"
        index
        :data="dataList"
        :default-sort="{ prop: 'num' }"
        @sort-change="handleSort"
      >
        <el-table-column label="编号" type="index" :index="indexAdd" width="50" align="center" />
        <el-table-column label="用户" align="center" prop="nickName" />
        <el-table-column label="用户账号" align="center" prop="mobile" />
        <el-table-column label="邀请人数" align="center" sortable="custom" prop="count" />
        <el-table-column label="待完成" align="center" prop="notPurchaseCount" />
        <el-table-column label="已完成" align="center" prop="purchaseCount" />
        <el-table-column label="获得奖励" align="center" width="120" fixed="right">
          <template #default="{ row }">
            <div>
              <el-link :underline="false" type="primary" @click="handleDetail(row)">详情</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <!-- 延长时间 -->
      <invitationDetail ref="detailRef" />
    </div>
  </el-dialog>
</template>

<script setup>
import { getJoinListApi } from '@/api/marketing/invitation'
import invitationDetail from './invitationDetail.vue'

const queryRef = ref(null)

const total = ref(0)
const dataList = ref([])
const visible = ref(false)

const queryParams = reactive({
  nickName: '',
  userName: '',
  marActivityId: '',
  pageNum: 1,
  pageSize: 10,
  sort: ''
})
const loading = ref(true)

// 表格编号递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams
  return index + 1 + (pageNum - 1) * pageSize
}

/** 查询用户列表 */
function getList() {
  loading.value = true
  getJoinListApi(queryParams)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

function handleSort(row) {
  queryParams.sort = row.order === 'descending' ? 'desc' : row.order === 'ascending' ? 'asc' : ''
  handleQuery()
}

// 邀请详情
const detailRef = ref(null)
function handleDetail(row) {
  detailRef.value.init(row.clientId, row.marActivityId)
}

function init(id) {
  visible.value = true
  queryParams.marActivityId = id
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
</style>
